package com.hllxd.hism.passport.login.gateway.dao.http;

import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Map;

/**
 * @author lixiyong
 * @date 2021.07.15
 */
public class HttpclientUtil {
    private static final CloseableHttpClient HTTP_CLIENT = HttpClientBuilder.create().build();
    private static final Logger logger = LoggerFactory.getLogger(HttpclientUtil.class);

    public static String get(String url){
        HttpGet httpGet = new HttpGet(url);
        // 响应模型
        CloseableHttpResponse response = null;
        try {
            // 由客户端执行(发送)Get请求
            response = HTTP_CLIENT.execute(httpGet);
            // 从响应模型中获取响应实体
            HttpEntity responseEntity = response.getEntity();
            logger.info("响应状态为:" + response.getStatusLine());
            if (responseEntity != null) {
                String tmp = EntityUtils.toString(responseEntity, StandardCharsets.UTF_8);
                logger.info("响应内容长度为:" + responseEntity.getContentLength());
                logger.info("响应内容为:" + tmp);
                return tmp;
            }
        } catch (ClientProtocolException e) {
            logger.error("ClientProtocolException: {}.", e.toString());
        } catch (ParseException e) {
            logger.error("ParseException: {}.",e.toString());
        } catch (IOException e) {
            logger.error("IOException: {}.", e.toString());
        }
        return null;
    }

    public static String post(String url, String data, Map<String, String> header){
        HttpPost httpPost = new HttpPost(url);
        header.forEach((key, value) -> {
            httpPost.setHeader(key, value);
        });
        if (null != data && data.length() > 0){
            StringEntity entity = new StringEntity(data, "UTF-8");
            httpPost.setEntity(entity);
        }
        // 响应模型
        CloseableHttpResponse response = null;
        try {
            // 由客户端执行(发送)Get请求
            response = HTTP_CLIENT.execute(httpPost);
            // 从响应模型中获取响应实体
            HttpEntity responseEntity = response.getEntity();
            logger.info("响应状态为:" + response.getStatusLine());
            if (responseEntity != null) {
                String tmp = EntityUtils.toString(responseEntity, StandardCharsets.UTF_8);
                logger.info("响应内容长度为:" + responseEntity.getContentLength());
                logger.info("响应内容为:" + tmp);
                return tmp;
            }
        } catch (ClientProtocolException e) {
            logger.error("ClientProtocolException: {}.",e.toString());
        } catch (ParseException e) {
            logger.error("ParseException: {}.",e.toString());
        } catch (IOException e) {
            logger.error("IOException: {}.",e.toString());
        }
        return null;
    }
}
